'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 联盟帮助
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var moment = require('moment');
var constantUtil = require('../../../../util/constantUtil');
var Code = require('../../../../consts/code');
var unionGroupModel = require('../../../../models/unionGroupModel');
var unionGroupConf = require('../../../../config/unionGroupConf');
var unionHelpConf = require('../../../../config/unionHelpConf.js');
var PlayerManager = require('../../../../libs/playerManager');
var buildType = require('../../../../consts/buildType.js');
var unionGroupMgr = require('../../../../mgr/unionGroupMgr.js');
var unionHelpModel = require('../../../../models/unionHelpModel.js');

/**
 * 请求联盟帮助
 */
module.exports = function (msg, session, next) {
    var role_id = session.uid;
    var share_id = msg.share_id;
    var help_type = msg.help_type;
    var union_group_model;
    var role_model;
    var help_max_num = 0;
    var union_id = 0;
    var add_help_msg;
    var only_item = null;

    // jshint maxstatements:150
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'build', 'tech', 'unionTask', 'accrued'], cb);
            },
            function (cb) {
                role_model = player_manager.role;
                union_id = role_model.union_id;
                if (!union_id) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }
                unionGroupModel.getById(union_id, function (err, model) {
                    union_group_model = model;
                    cb();
                });
            },
            function (cb) {
                var help_list2 = {
                    union_id: union_id,
                    role_id: role_id,
                    share_id: share_id,
                    help_type: help_type
                };

                unionHelpModel.findUnionHelpItem(help_list2, function (err, item) {
                    only_item = item;
                    if (!!only_item) {
                        return next(null, {
                            code: Code.UNION_IS_HELP_YES
                        });
                    }
                    cb();
                });
            },

            function (cb) {
                if (!union_group_model) {
                    return next(null, {
                        code: Code.UNION_IS_NOT_JOIN
                    });
                }

                var build_model = player_manager.build;
                var nexue_lv = build_model.getBuildByType(buildType.NEXUS).lv;

                // help_type : 1--建筑     2--科技    3--联盟任务
                if (help_type == 1) {
                    var build_id = share_id;
                    var build_item = build_model.getBuildSchedule(build_id);
                    var build = build_model.getBuild(build_id);

                    // 建筑队列是否存在
                    if (!build_item) {
                        return next(null, {
                            code: Code.BUILD_NOT_SCHE
                        });
                    }

                    // 建筑队列是否存在
                    if (!build) {
                        return next(null, {
                            code: Code.BUILD_NOT_EXIST
                        });
                    }

                    help_max_num = unionHelpConf.getHelpNumConf(nexue_lv);
                    add_help_msg = [{
                        union_id: union_id,
                        role_id: role_id,
                        share_id: build_id,
                        job: role_model.getJob(),
                        name: role_model.getName(),
                        lv: build.lv,
                        help_type: help_type,
                        end_time: moment(build_item.end_time).unix(),
                        help_max_num: help_max_num,
                        help_max_times: help_max_num
                    }];
                }

                // 科技帮助请求
                if (help_type == 2) {
                    var tech_id = share_id;
                    var tech_model = player_manager.tech;

                    // 科技是否在队列中
                    if (tech_model.up_tech_id !== tech_id) {
                        return next(null, {
                            code: Code.TECH_QUE_EMPTY
                        });
                    }

                    help_max_num = unionHelpConf.getHelpNumConf(nexue_lv);
                    add_help_msg = [{
                        union_id: union_id,
                        role_id: role_id,
                        share_id: tech_id,
                        job: role_model.getJob(),
                        name: role_model.getName(),
                        lv: tech_model.getTechLvl(tech_id),
                        help_type: help_type,
                        end_time: tech_model.up_tech_end_tm,
                        help_max_num: help_max_num,
                        help_max_times: help_max_num
                    }];
                }
                cb();
            },
            function (cb) {
                // 推送
                msg = {
                    help_list: add_help_msg,
                    is_modify: 1
                };

                unionGroupMgr.sendUnioHelpChangedMulti(
                    union_id,
                    msg,
                    function (err, fails) {
                        if (err) {
                            console.error('send unionGroup message error: %j, fail ids: %j', err, fails);
                            return;
                        }
                        cb();
                    }
                );
            },
            function (cb) {
                //增加记录
                unionHelpModel.createHelp(add_help_msg[0], function (err) {
                    cb();
                });
                //cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
